package jsqlite.JDBC2z;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import jsqlite.BusyHandler;
import jsqlite.Database;
import jsqlite.Exception;
import jsqlite.JDBCDriver;

/* loaded from: classes2.dex */
public class JDBCConnection implements Connection, BusyHandler {
    protected DatabaseX db;
    private String dbfile;
    protected String enc;
    private long t0;
    protected String url;
    protected boolean useJulian;
    protected String vfs;
    protected boolean autocommit = true;
    protected boolean intrans = false;
    protected int timeout = 1000000;
    private JDBCDatabaseMetaData meta = null;
    private boolean readonly = false;
    private int trmode = 8;

    @Override // java.sql.Connection
    public void clearWarnings() throws SQLException {
    }

    @Override // java.sql.Connection
    public String getCatalog() throws SQLException {
        return null;
    }

    @Override // java.sql.Connection
    public int getHoldability() throws SQLException {
        return 1;
    }

    @Override // java.sql.Connection
    public SQLWarning getWarnings() throws SQLException {
        return null;
    }

    @Override // java.sql.Connection
    public boolean isValid(int i) throws SQLException {
        return true;
    }

    @Override // java.sql.Wrapper
    public boolean isWrapperFor(Class cls) throws SQLException {
        return false;
    }

    @Override // java.sql.Connection
    public void setCatalog(String str) throws SQLException {
    }

    private boolean busy0(DatabaseX databaseX, int i) {
        if (i <= 1) {
            this.t0 = System.currentTimeMillis();
        }
        if (databaseX == null || System.currentTimeMillis() - this.t0 > this.timeout) {
            return false;
        }
        databaseX.wait(100);
        return true;
    }

    @Override // jsqlite.BusyHandler
    public boolean busy(String str, int i) {
        return busy0(this.db, i);
    }

    public boolean busy3(DatabaseX databaseX, int i) {
        if (i <= 1) {
            this.t0 = System.currentTimeMillis();
        }
        return databaseX != null && System.currentTimeMillis() - this.t0 <= ((long) this.timeout);
    }

    private DatabaseX open(boolean z) throws SQLException {
        try {
            DatabaseX databaseX = new DatabaseX();
            databaseX.open(this.dbfile, z ? 1 : 6, this.vfs);
            databaseX.set_encoding(this.enc);
            int i = 0;
            do {
                try {
                    databaseX.exec("PRAGMA short_column_names = off;", null);
                    databaseX.exec("PRAGMA full_column_names = on;", null);
                    databaseX.exec("PRAGMA empty_result_callbacks = on;", null);
                    if (Database.version().compareTo("2.6.0") >= 0) {
                        databaseX.exec("PRAGMA show_datatypes = on;", null);
                    }
                    return databaseX;
                } catch (Exception e) {
                    if (databaseX.last_error() != 5) {
                        databaseX.close();
                        throw new SQLException(e.toString());
                    }
                    i++;
                }
            } while (!busy0(databaseX, i));
            try {
                databaseX.close();
            } catch (Exception unused) {
            }
            throw new SQLException(e.toString());
        } catch (Exception e2) {
            throw new SQLException(e2.toString());
        }
    }

    public JDBCConnection(String str, String str2, String str3, String str4, String str5) throws SQLException {
        boolean z = true;
        this.useJulian = false;
        this.dbfile = null;
        if (str.startsWith("jsqlite:/")) {
            this.dbfile = str.substring(8);
        } else if (str.startsWith("jdbc:jsqlite:/")) {
            this.dbfile = str.substring(13);
        } else {
            throw new SQLException("unsupported url");
        }
        this.url = str;
        this.enc = str2;
        this.vfs = str5;
        try {
            this.db = open(this.readonly);
            if (str3 != null) {
                try {
                    if (str3.length() > 0) {
                        this.db.key(str3);
                    }
                } catch (Exception unused) {
                    throw new SQLException("error while setting key");
                }
            }
            this.db.busy_handler(this);
            if (str4 == null || (!str4.startsWith("j") && !str4.startsWith("J"))) {
                z = false;
            }
            this.useJulian = z;
        } catch (SQLException e) {
            DatabaseX databaseX = this.db;
            if (databaseX != null) {
                try {
                    databaseX.close();
                } catch (Exception unused2) {
                }
            }
            throw e;
        }
    }

    public Database getSQLiteDatabase() {
        return this.db;
    }

    @Override // java.sql.Connection
    public Statement createStatement() {
        return new JDBCStatement(this);
    }

    @Override // java.sql.Connection
    public Statement createStatement(int i, int i2) throws SQLException {
        if (i == 1003 || i == 1004 || i == 1005) {
            if (i2 != 1007 && i2 != 1008) {
                throw new SQLFeatureNotSupportedException("unsupported result set concurrency");
            }
            return new JDBCStatement(this);
        }
        throw new SQLFeatureNotSupportedException("unsupported result set type");
    }

    @Override // java.sql.Connection
    public DatabaseMetaData getMetaData() throws SQLException {
        if (this.meta == null) {
            this.meta = new JDBCDatabaseMetaData(this);
        }
        return this.meta;
    }

    @Override // java.sql.Connection, java.lang.AutoCloseable
    public void close() throws SQLException {
        try {
            rollback();
        } catch (SQLException unused) {
        }
        this.intrans = false;
        DatabaseX databaseX = this.db;
        if (databaseX != null) {
            try {
                databaseX.close();
                this.db = null;
            } catch (Exception e) {
                throw new SQLException(e.toString());
            }
        }
    }

    @Override // java.sql.Connection
    public boolean isClosed() throws SQLException {
        return this.db == null;
    }

    @Override // java.sql.Connection
    public boolean isReadOnly() throws SQLException {
        return this.readonly;
    }

    @Override // java.sql.Connection
    public void commit() throws SQLException {
        DatabaseX databaseX = this.db;
        if (databaseX == null) {
            throw new SQLException("stale connection");
        }
        if (this.intrans) {
            try {
                databaseX.exec("COMMIT", null);
                this.intrans = false;
            } catch (Exception e) {
                throw new SQLException(e.toString());
            }
        }
    }

    @Override // java.sql.Connection
    public boolean getAutoCommit() throws SQLException {
        return this.autocommit;
    }

    @Override // java.sql.Connection
    public int getTransactionIsolation() throws SQLException {
        return this.trmode;
    }

    @Override // java.sql.Connection
    public String nativeSQL(String str) throws SQLException {
        throw new SQLException("not supported");
    }

    @Override // java.sql.Connection
    public CallableStatement prepareCall(String str) throws SQLException {
        throw new SQLException("not supported");
    }

    @Override // java.sql.Connection
    public CallableStatement prepareCall(String str, int i, int i2) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public PreparedStatement prepareStatement(String str) throws SQLException {
        return new JDBCPreparedStatement(this, str);
    }

    @Override // java.sql.Connection
    public PreparedStatement prepareStatement(String str, int i, int i2) throws SQLException {
        if (i == 1003 || i == 1004 || i == 1005) {
            if (i2 != 1007 && i2 != 1008) {
                throw new SQLFeatureNotSupportedException("unsupported result set concurrency");
            }
            return new JDBCPreparedStatement(this, str);
        }
        throw new SQLFeatureNotSupportedException("unsupported result set type");
    }

    @Override // java.sql.Connection
    public void rollback() throws SQLException {
        DatabaseX databaseX = this.db;
        if (databaseX == null) {
            throw new SQLException("stale connection");
        }
        if (this.intrans) {
            try {
                databaseX.exec("ROLLBACK", null);
                this.intrans = false;
            } catch (Exception e) {
                throw new SQLException(e.toString());
            }
        }
    }

    @Override // java.sql.Connection
    public void setAutoCommit(boolean z) throws SQLException {
        DatabaseX databaseX;
        if (z && this.intrans && (databaseX = this.db) != null) {
            try {
                try {
                    databaseX.exec("ROLLBACK", null);
                } catch (Exception e) {
                    throw new SQLException(e.toString());
                }
            } finally {
                this.intrans = false;
            }
        }
        this.autocommit = z;
    }

    @Override // java.sql.Connection
    public void setReadOnly(boolean z) throws SQLException {
        DatabaseX open;
        if (this.intrans) {
            throw new SQLException("incomplete transaction");
        }
        if (z != this.readonly) {
            DatabaseX databaseX = null;
            try {
                try {
                    open = open(z);
                } catch (Exception e) {
                    e = e;
                }
                try {
                    this.db.close();
                    this.db = open;
                    this.readonly = z;
                } catch (Exception e2) {
                    e = e2;
                    databaseX = open;
                    if (databaseX != null) {
                        try {
                            databaseX.close();
                        } catch (Exception unused) {
                        }
                    }
                    throw new SQLException(e.toString());
                }
            } catch (SQLException e3) {
                throw e3;
            }
        }
    }

    @Override // java.sql.Connection
    public void setTransactionIsolation(int i) throws SQLException {
        String str;
        if (this.db.is3() && JDBCDriver.sharedCache) {
            if (i != 1 || this.trmode == 1) {
                str = (i != 8 || this.trmode == 8) ? null : "off";
            } else {
                str = "on";
            }
            if (str != null) {
                try {
                    DatabaseX databaseX = this.db;
                    databaseX.exec("PRAGMA read_uncommitted = " + str + ";", null);
                    this.trmode = i;
                } catch (Exception unused) {
                }
            }
        }
        if (i != this.trmode) {
            throw new SQLException("not supported");
        }
    }

    @Override // java.sql.Connection
    public Map<String, Class<?>> getTypeMap() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public void setTypeMap(Map map) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public void setHoldability(int i) throws SQLException {
        if (i != 1) {
            throw new SQLFeatureNotSupportedException("unsupported holdability");
        }
    }

    @Override // java.sql.Connection
    public Savepoint setSavepoint() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public Savepoint setSavepoint(String str) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public void rollback(Savepoint savepoint) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public Statement createStatement(int i, int i2, int i3) throws SQLException {
        if (i3 != 1) {
            throw new SQLFeatureNotSupportedException("unsupported holdability");
        }
        return createStatement(i, i2);
    }

    @Override // java.sql.Connection
    public PreparedStatement prepareStatement(String str, int i, int i2, int i3) throws SQLException {
        if (i3 != 1) {
            throw new SQLFeatureNotSupportedException("unsupported holdability");
        }
        return prepareStatement(str, i, i2);
    }

    @Override // java.sql.Connection
    public CallableStatement prepareCall(String str, int i, int i2, int i3) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public PreparedStatement prepareStatement(String str, int i) throws SQLException {
        if (i != 2) {
            throw new SQLFeatureNotSupportedException("generated keys not supported");
        }
        return prepareStatement(str);
    }

    @Override // java.sql.Connection
    public PreparedStatement prepareStatement(String str, int[] iArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public PreparedStatement prepareStatement(String str, String[] strArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public Clob createClob() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public Blob createBlob() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public NClob createNClob() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public SQLXML createSQLXML() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public void setClientInfo(String str, String str2) throws SQLClientInfoException {
        throw new SQLClientInfoException();
    }

    @Override // java.sql.Connection
    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        throw new SQLClientInfoException();
    }

    @Override // java.sql.Connection
    public String getClientInfo(String str) throws SQLException {
        throw new SQLException("unsupported");
    }

    @Override // java.sql.Connection
    public Properties getClientInfo() throws SQLException {
        return new Properties();
    }

    @Override // java.sql.Connection
    public Array createArrayOf(String str, Object[] objArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Connection
    public Struct createStruct(String str, Object[] objArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Wrapper
    public <T> T unwrap(Class<T> cls) throws SQLException {
        throw new SQLException("unsupported");
    }
}
